package it.traveldream.ManagedBeans;


import it.traveldream.EJB.UtenteEJB;
import it.traveldream.EJB.DTO.UtenteDTO;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;



@ManagedBean(name="utenteBean")
@SessionScoped
public class UserBean {

	private UtenteDTO user;

	@EJB
	private UtenteEJB utenteEjb;


	private String errorMessage;

	public UserBean(){

		user = new UtenteDTO();

	}



	public void addUser(){


		System.out.println(user.getNome());
		System.out.println(user.getCognome());
		System.out.println(user.getUsername());
		System.out.println(user.getEmail());
		System.out.println(user.getDataNascita());
		System.out.println(user.getPassword());


		if(user.getRep_password().length()== 0 || user.getPassword().length() == 0)
			errorMessage = "Completare tutti i campi";
		else{
			if(!user.getPassword().equalsIgnoreCase(user.getRep_password()))
				errorMessage = "Password Errata!";
			else{
				try {
					utenteEjb.addUser(user);
					FacesContext.getCurrentInstance().addMessage("growl", new FacesMessage("Registrato", "Registrazione avvenuta con successo"));
				} catch (Exception e) {

					FacesContext.getCurrentInstance().addMessage("growl",
							new FacesMessage("Errore nel recupero delle informazioni", e.getMessage() == null ? "" : e.getMessage()));
				}
			}
		}
	}


	//GETTERS AND SETTERS



	public UtenteDTO getUser() {
		return user;
	}



	public void setUser(UtenteDTO user) {
		this.user = user;
	}


	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}






	//GETTERS AND SETTERS - FINE


}
